const BlogModel = require('../../models/BlogModel.js')

const BlogService = {
  add: async ({ title, content, category, cover, isPublish, updateTime }) => {
    return BlogModel.create({ title, content, category, cover, isPublish, updateTime })
  },
  list: async ({ _id }) => {
    return _id ? BlogModel.find({ _id }) : BlogModel.find({})
  },
  publish: async ({ _id, isPublish, updateTime }) => {
    return BlogModel.updateOne({
      _id
    }, {
      isPublish,
      updateTime
    })
  },
  delete: async ({ _id }) => {
    return BlogModel.deleteOne({ _id })
  },
  update: async ({ _id,title, content, category, cover, isPublish, updateTime }) => {
    if(cover) {
      return BlogModel.updateOne({_id},{ title, content, category, cover, isPublish, updateTime })
    } else {
      return BlogModel.updateOne({ _id }, { title, content, category, isPublish, updateTime })
    }
  },
}

module.exports = BlogService